package com.tzstcl.sys.quartz.ctrl;

import com.github.pagehelper.PageInfo;
import com.tzstcl.base.ctrl.BaseCtrl;
import com.tzstcl.base.model.AjaxResult;
import com.tzstcl.sys.quartz.model.Job;
import com.tzstcl.sys.quartz.service.impl.JobServiceImpl;
import com.tzstcl.sys.quartz.utils.QuartzManager;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * 公司：天筑科技股份有限公司
 * 作者：zhugq
 * 日期：2018年09月20日
 * 说明：定时任务Controller
 */
@Controller
@RequestMapping("/admin/job")
public class JobCtrl extends BaseCtrl {

    @Autowired
    private JobServiceImpl jobServiceImpl;
    @Autowired
    QuartzManager quartzManager;

    /**
     * 新增 修改
     *
     * @param job
     * @return
     */
    @RequestMapping("/save")
    @RequiresPermissions(value = {"job:edite", "job:add"}, logical = Logical.OR)
    @ResponseBody
    public AjaxResult save(@Valid Job job) {
        int res = 0;
        if (null == job.getJobId()) {
            res = jobServiceImpl.add(job);
        } else {
            res = jobServiceImpl.update(job);
        }
        return toAjax(res);
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    @RequiresPermissions("job:delete")
    @ResponseBody
    public AjaxResult delete(String ids) {
        return toAjax(jobServiceImpl.deleteBatchIds(ids));
    }

    /**
     * 获取单条信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/get")
    @RequiresPermissions("job:view")
    @ResponseBody
    public AjaxResult get(Long id) {
        return success("获取信息成功", jobServiceImpl.getOne(id));
    }

    /**
     * 获取查询的分页数据
     *
     * @param job
     * @return
     */
    @RequestMapping("/list")
    @RequiresPermissions("job:view")
    @ResponseBody
    public PageInfo<Job> list(Job job) {
        return jobServiceImpl.selectPage(job);
    }

    /**
     * list页面导航
     *
     * @return
     */
    @RequiresPermissions("job:view")
    @RequestMapping("/toList")
    public String toList() {
        return "admin/sys/quartz/jobList";
    }

    /**
     * form页面导航
     *
     * @return
     */
    @RequiresPermissions(value = {"job:edite", "job:add"}, logical = Logical.OR)
    @RequestMapping("/toForm")
    public String toForm() {
        return "admin/sys/quartz/jobForm";
    }


    /**
     * 执行某一个定时任务方案
     *
     * @return
     */
    @RequestMapping("/executePlan")
    @RequiresPermissions(value = {"job:edite", "job:add"}, logical = Logical.OR)
    @ResponseBody
    public AjaxResult executePlan(Long id) {
        Job job = jobServiceImpl.getOne(id);
        Class clzz = null;
        try {
            clzz = Class.forName(job.getJobClassPath());
        } catch (Exception e) {
            e.printStackTrace();
            return error();
        }
        quartzManager.addOrUpdateJob(clzz, job.getJobName(), job.getJobGroup(), job.getCronExpression());
        return success();
    }

    /**
     * 删除某一个定时任务方案
     *
     * @return
     */
    @RequestMapping("/deleteJob")
    @RequiresPermissions(value = {"job:edite", "job:add"}, logical = Logical.OR)
    @ResponseBody
    public AjaxResult deleteJob(Long id) {
        Job job = jobServiceImpl.getOne(id);
        quartzManager.deleteJob(job.getJobName(), job.getJobGroup());
        return success();
    }

    /**
     * 暂停某一个定时任务方案
     *
     * @return
     */
    @RequestMapping("/pauseJob")
    @RequiresPermissions(value = {"job:edite", "job:add"}, logical = Logical.OR)
    @ResponseBody
    public AjaxResult pauseJob(Long id) {
        Job job = jobServiceImpl.getOne(id);
        job.setStatus(Job.STATUS_PAUSE);
        int res = jobServiceImpl.update(job);
        if (res > 0) {
            quartzManager.pauseJob(job.getJobName(), job.getJobGroup());
        }
        return success();
    }

    /**
     * 回复某暂停某的定时任务方案
     *
     * @return
     */
    @RequestMapping("/resumeJob")
    @RequiresPermissions(value = {"job:edite", "job:add"}, logical = Logical.OR)
    @ResponseBody
    public AjaxResult resumeJob(Long id) {

        Job job = jobServiceImpl.getOne(id);
        job.setStatus(Job.STATUS_RESUME);
        int res = jobServiceImpl.update(job);
        if (res > 0) {
            quartzManager.resumeJob(job.getJobName(), job.getJobGroup());
        }
        return success();
    }

    /**
     * 立即执行定时任务
     *
     * @return
     */
    @RequestMapping("/runAJobNow")
    @RequiresPermissions(value = {"job:edite", "job:add"}, logical = Logical.OR)
    @ResponseBody
    public AjaxResult runAJobNow(Long id) {
        Job job = jobServiceImpl.getOne(id);
        quartzManager.runAJobNow(job.getJobName(), job.getJobGroup());
        return success();
    }

    /**
     * 获取所有的任务
     *
     * @return
     */
    @RequestMapping("/queryAllJob")
    @RequiresPermissions("job:view")
    @ResponseBody
    public AjaxResult queryAllJob() {
        return success("获取成功", quartzManager.queryAllJob());
    }

    /**
     * 获取正在执行的任务
     *
     * @return
     */
    @RequestMapping("/queryRunJob")
    @RequiresPermissions("job:view")
    @ResponseBody
    public AjaxResult queryRunJob() {
        return success("获取成功", quartzManager.queryRunJob());
    }

}